function BBCode(textBox)
{
    this.textBox = textBox;
    this.openTags = [];
    this.support = new TextBoxSupport(textBox);
    
    this.tagPosition = function(tag)
    {
	    var i = this.openTags.length;
	    while (i >= 0)
	    {
		    if (this.openTags[i] == tag)
		    {
                return i;
		    }
		    i--;
	    }
	    return -1;
    }

    this.closeAll = function()
    {
        for (i = this.openTags.length - 1; i >= 0; i--)
        {
            this.textBox.value += "[/" + this.openTags[i] + "]";
            this.openTags.splice(i, 1);
        }
        this.textBox.focus();
    }

    this.addEmoticon = function(emoticon)
    {
        this.support.replaceSelection(emoticon);
        this.textBox.focus();
    }
    
    this.addFontStyle = function(attributeType, value)
    {
        this.textBox.focus();
        var range = document.selection.createRange();
        if (range.text != "" && this.textBox.value.indexOf(range.text) >= 0)
        {
            range.text = "[" + attributeType + "=" + value + "]" + range.text + "[/" + attributeType + "]";
        }
        else
        {
            range.text = "[" + attributeType + "=" + value + "][/" + attributeType + "]";
        }
        this.textBox.focus();
    }

    this.addTag = function(tag)
    {
        var selectedText = this.support.getSelectedText();
        if (selectedText != "")
        {
            this.support.replaceSelection("[" + tag + "]" + selectedText + "[/" + tag + "]");
            var position = this.textBox.value.indexOf(selectedText) + selectedText.length;
            this.support.moveCursor(position);
        }
        else
        {
            var tagPosition = this.tagPosition(tag);
            if (tagPosition == -1)
            {
                this.support.replaceSelection("[" + tag + "]");
                this.openTags[this.openTags.length] = tag;
            }
            else
            {
                this.support.replaceSelection("[/" + tag + "]");
                this.openTags.splice(tagPosition, 1);
            }
        }
        this.textBox.focus();
    }
}

function TextBoxSupport(textBox)
{
    this.textBox = textBox;
    
    this.replaceSelection = function(text)
    {
        if (document.selection)
        {
            this.textBox.focus();
            var range = document.selection.createRange();
            range.text = text;
            this.textBox.focus();
        }
        else
        {
            var start = this.textBox.selectionStart;
            var end = this.textBox.selectionEnd;
            this.textBox.value = this.textBox.value.substr(0, start) + text + this.textBox.value.substr(end, this.textBox.value.length);
        }
    }
    
    this.getSelectedText = function()
    {
        if (document.selection)
        {
            var range = document.selection.createRange();
            return range.text;
        }
        else
        {
            var start = this.textBox.selectionStart;
            var end = this.textBox.selectionEnd;
            return this.textBox.value.substr(start, end - start);
        }
    }
    
    this.moveCursor = function(position)
    {   
        if (this.textBox.createTextRange)
        {
            var range = this.textBox.createTextRange();
            range.move("character", position);
            range.select();
        }
        else
        {
            this.textBox.setSelectionRange(position, position);
        }
    }   
}
